package com.yidu.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnUserMapper;
import com.yidu.dao.SnUserRoleMapper;
import com.yidu.model.SnBranch;
import com.yidu.model.SnProduction;
import com.yidu.model.SnRole;
import com.yidu.model.SnUser;
import com.yidu.model.SnUserRole;
import com.yidu.service.SnUserService;

/**
 * 
 * 描述:用户表的实类
 * @author 骆军
 * @下午2:47:53
 */
@Service
public class SnUserServiceImpl implements SnUserService{
	
	/**
	 * 用户dao
	 */
	@Resource
	SnUserMapper snUserMapperDao;
	
	
	/**
	 * 用户角色表dao
	 */
	@Resource
	SnUserRoleMapper snDao;
	
	/**
	 * 查询显示所有
	 */
	@Override
	public List<SnUser> selectAll(Map<String, Object> map) {
		return snUserMapperDao.selectAll(map);
	}
	
	/**
	 * 查询行数
	 */
	@Override
	public int findCount(Map<String, Object> maps) {
		return snUserMapperDao.findCount(maps);
	}

	/**
	 * 根据id查询
	 */
	@Override
	public SnUser selectByPrimaryKey(String userId) {
		return snUserMapperDao.selectByPrimaryKey(userId);
	}
	
	/**
	 * 根据id修改
	 */
	@Override
	public int updateByPrimaryKeySelective(SnUser record) {
		return snUserMapperDao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 增加或修改
	 */
	@Override
	public int addorUpdate(SnUser user,String id,HttpSession session) {
		int rows=0;
		if(user.getUserId()!=null && !"".equals(user.getUserId())){
			//根据用户id删除用户角色表
			snDao.deleteByPrimaryKey(user.getUserId());
			//获取到参数id 根据@分割
			String[] str =id.split("@");
			//new用户角色表
			SnUserRole userSnrole=new SnUserRole();
			for (int i = 0; i < str.length; i++) {
				String uuid=UUID.randomUUID().toString().replaceAll("-","");
				userSnrole.setUrId(uuid);
				//给用户表id赋值
				userSnrole.setUserId(user.getUserId());
				//给用户角色id赋值
				userSnrole.setRoId(str[i]);
				//在调用用户角色表的增加方法
				snDao.insertSelective(userSnrole);
			}
			//取到当前登陆的session
			SnUser users = (SnUser) session.getAttribute("user");
			//通过session给操作人赋值
			user.setOper(users.getUserName());
			//在调用用户表的修改方法
			rows=snUserMapperDao.updateByPrimaryKeySelective(user);
			
		}else{
			String uuidOne=UUID.randomUUID().toString().replaceAll("-","");
			//将数据设置为有效
			user.setIsva(1);
			//给用户id赋值
			user.setUserId(uuidOne);
			user.setSort(Integer.valueOf(Tools.getTimeStamp()));
			rows=snUserMapperDao.insertSelective(user);
			//获取到参数id 根据@分割
			String[] str =id.split("@");
			//new用户角色表
			SnUserRole userSnrole=new SnUserRole();
			for (int i = 0; i < str.length; i++) {
				String uuid=UUID.randomUUID().toString().replaceAll("-","");
				userSnrole.setUrId(uuid);
				//给用户表id赋值
				userSnrole.setUserId(user.getUserId());
				//给用户角色id赋值
				userSnrole.setRoId(str[i]);
				//在调用用户角色表的增加方法
				snDao.insertSelective(userSnrole);
			}
		}
		return rows;
	}

	/**
	 * 查询显示所有
	 */
	@Override
	public List<SnRole> findAll() {
		return snUserMapperDao.findAll();
	}

	/**
	 * 查询角色类型
	 */
	@Override
	public List<SnUser> query() {
		//调用查询所有的方法
		return snUserMapperDao.query();
	}

	/**
	 * 查询分公司下拉框
	 */
	@Override
	public List<SnBranch> findbyName() {
		// TODO Auto-generated method stub
		return snUserMapperDao.findbyName();
	}

	

	/**
	 * 修改获取角色权限
	 */
	@Override
	public List<SnUserRole> selectName(String id) {
		// TODO Auto-generated method stub
		return snUserMapperDao.selectName(id);
	}

	/**
	 * 判断用户名是否存在
	 */
	@Override
	public int selectOnlin(String name) {
		return snUserMapperDao.selectOnlin(name);
	}

}
